<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏
    </el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" @keydown.enter="loadGeofence"
                  v-model="searchGeofenceForm.name"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" @change="loadGeofence" v-model="searchGeofenceForm.status">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeofence">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geofenceArr">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true"
                       width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"
                     @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeofence(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="showUnbindVehicleList">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <!-- :before-close="makeSure"要在关闭新添加绑定车辆的弹窗后立即加载已绑定车辆的弹窗表格数据-->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handleClose">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"
                       :formatter="VehicleStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindNewVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import {ElMessage} from "element-plus";
import axios from "axios";
import router from "@/router";


// 控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
// 控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);


// 定义数组保存查询电子围栏列表信息
const geofenceArr = ref([]);
// 定义对象保存查询条件 围栏名称 围栏状态
const searchGeofenceForm = ref({name: '', status: ''});

// 定义加载电子围栏信息的方法
const loadGeofence = () => {
  // 以查询字符串的格式打印用户输入的搜索条件
  let data = qs.stringify(searchGeofenceForm.value);
  console.log(data);
  // 从后端查询数据，加载到页面上
  axios.get(BASE_URL + '/v1/geofence/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      geofenceArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

// 车辆类型文字转换，转换为文字显示
const vehicleTypeFormatter = (row, colum, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '轿车';
  }
  if (cellValue == 20) {
    cellValue = '货车';
  }
  if (cellValue == 30) {
    cellValue = '客车';
  }
  if (cellValue == 40) {
    cellValue = '挂车';
  }
  return cellValue;
}

// 车辆状态转换文字显示
const VehicleStatusFormatter = (row, colum, cellValue, index) => {
  if (cellValue == 1) {
    cellValue = '未绑定'
  }
  if (cellValue == 0) {
    cellValue = '已绑定'
  }
  return cellValue;
}

// 定义一个全局变量id，这个是保存当前电子围栏的id，用于解绑电子围栏车辆
var geoId;

// 一进到页面立即加载所有的电子围栏信息数据
onMounted(() => {
  loadGeofence();
});

// 重置按钮
const resetSearch = () => {
  // 清空查询内容
  searchGeofenceForm.value = {name: '', status: ''};//这里不能用''会出错误
  // 重新加载所有表单信息
  loadGeofence();
};

// 定义数组用来保存当前围栏已绑定的车辆信息
const bindVehicleArr = ref([]);
// 定义方法加载当前围栏已绑定的车辆数据
const loadVehicle = (geofenceId) => {
  // 显示已绑定车辆弹窗
  bindVehicleDialogVisible.value = true;
  // 将传过来的当前围栏的id赋值给全局变量
  geoId = geofenceId;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId=' + geofenceId).then((response) => {
    if (response.data.code === 2000) {
      // 显示已绑定车辆
      bindVehicleArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

// 移除，此处的id是车辆id
const removeBindVehicle = (id) => {
  axios.post(BASE_URL + '/v1/vehicle/unbind/' + id).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('车辆解绑成功')
      // 重新显示当前所有已绑定车辆信息
      loadVehicle(geoId);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

// 定义数组用来保存可绑定围栏的可选车辆信息
const unBindVehicleArr = ref([])

// 显示可绑定车辆
const showUnbindVehicleList = () => {
  // 显示"可绑定车辆列表"
  AddBindVehicleDialogVisible.value = true;
  // 从后端查询所有未绑定围栏的车辆信息
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0').then((response) => {
    if (response.data.code === 2000) {
      unBindVehicleArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

// 绑定,此处id是车辆id
const bindNewVehicle = (id) => {
  axios.post(BASE_URL + '/v1/vehicle/bind/' + geoId + '/' + id).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('绑定成功！');
      // 重新加载一下未绑定的车辆
      showUnbindVehicleList();
      // 优化代码，没绑定一列车，就重新加载所有可绑定车辆的数据
      handleClose();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

// 关闭内层弹窗（可绑定车辆弹窗）的处理方法
const handleClose = () => {
  // 加载当前围栏的已绑定车辆数据
  loadVehicle(geoId);
  // 在关闭内层可绑定车辆弹窗
  AddBindVehicleDialogVisible.value = false;
}


// 删除围栏信息按钮
const deleteGeofence = (geofenceId) => {
  axios.post(BASE_URL + '/v1/geofence/delete/' + geofenceId).then((response) => {
    if (confirm('你确定删除吗')) {
      if (response.data.code === 2000) {
        ElMessage.success('删除成功！');
        // 立即刷新当前页面，重新显示数据
        loadGeofence();
      } else {
        // ElMessage.error(response.data.msg);
        ElMessage.error('请先移除所有已绑定车辆！');
      }
    }
  })
}

//修改围栏状态的方法 (围栏id,围栏状态)
const changeStatus = (id, status) => {
  let data = qs.stringify({id: id, status: status});
  axios.post(BASE_URL + '/v1/geofence/save', data).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('修改围栏状态成功!');
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

</script>

<style scoped>
</style>